<?php
/**
 * @copyright Arthur Beer
 * @package core.config
 *
 * @copyright TinyWEB CMS
 * @link http://tinywebcms.ru
 * @link http://cms.vingrad.ru
 * @version SVN: $Id$ 
 */
 
 /**
  * ConfigProperty
  */
class ConfigProperty extends Object implements Iterator
{
    private static $instances;
    
    /**
     * SimpleXML
     *
     * @var SimpleXMLElement
     */
    private $node;
    
    private $children;
    private $count;
    private $i;
    
    /**
     * Конструктор класса
     * 
     */
    public function __construct($node)
    {
        $this->node = $node;
    }
    
    /**
     * ConfigProperty
     *
     * @param string $name
     * @return ConfigProperty
     */
    public function __get($name)
    {
    	if($name == 'value') {
    		return (string) $this->node;
    	}
    	
    	if(!$this->node || empty($this->node->$name)) {
    		throw new Exception("ConfigProperty: Undefined node or not exists property '$name'.");
    	}
    	
        return new ConfigProperty($this->node->$name);
    }
    
    public function toString()
    {
        return (string) $this->node;
    }
    
    public function getName()
    {
        return $this->node->getName();
    }
    
    public function getAttributes()
    {
    	foreach ($this->node->attributes() as $key => $value) {
    		$attributes[$key] = (string) $value;
    	}
    	
    	return $attributes;
    }
    
    public function exists($name)
    {
    	return !empty($this->node->$name);
    }
    
    public function rewind()
    {
    	$this->i = 0;
    	$this->children = $this->node->children();
    	$this->count = count($this->children);
    }
    
    public function valid()
    {
    	return $this->i < $this->count;
    }
    
    public function current()
    {
    	return new ConfigProperty($this->children[$this->i]);
    }
    
    public function next()
    {
    	$this->i++;
    	return new ConfigProperty($this->children[$this->i]);
    }
    
    public function key()
    {
    	return $this->i;
    }
    
    public function find($xpath)
    {
        $elements = $this->node->xpath($xpath);
        if(is_array($elements) && count($elements)) {
            return new ConfigProperty($elements[0]);
        }
        
        return null;
    }
}

/**
 * Tab: 4 spaces
 * Encoding: utf-8
 */
?>